package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入序号");
        System.out.println("1:用户注册");
        System.out.println("2:用户登录");
        System.out.println("3:修改密码");
        System.out.println("4:用户列表");
        int num = scanner.nextInt();
        switch(num){
            case 1 : regUser();
            break;
            case 2 : loginUser();
            break;
            case 3 : changePassword();
            break;
            case 4 : listUser();
        }
    }
    public void regUser(){
        System.out.println("UserMapper:开始用户注册...");
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄:");
        int age = scanner.nextInt();
        UserInfo userinfo = new UserInfo(username,password,nickname,age);
        int num = userMapper.insert(userinfo);
        System.out.println(num>0?"注册成功":"注册失败");
    }
    public void loginUser(){
        System.out.println("UserMapper:开始登录...");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        UserInfo info = userMapper.select(username);
        if (info!=null) {
            if (info.getPassword().equals(password)) {
                System.out.println("登陆成功");
            }else{
                System.out.println("登录失败");
            }
        }else{
            System.out.println("该用户不存在");
        }
    }
    public void changePassword(){
        System.out.println("UserMapper:请验证用户名,密码");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        UserInfo info = userMapper.select(username);
        if (info!=null) {
            if (info.getPassword().equals(password)) {
                System.out.println("验证成功,请输入新密码");
                String newPassword = scanner.nextLine();
                int num = userMapper.change(password,newPassword);
                System.out.println(num>0?"修改成功":"修改失败");
            }else{
                System.out.println("登录失败");
            }
        }else{
            System.out.println("该用户不存在");
        }
    }
    public void listUser(){
        List<UserInfo> userlist = userMapper.selectAll();
        for (UserInfo userInfo : userlist){
            System.out.println(userInfo);
        }
    }
}
